﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DTO;
using BLL;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace SearchEngine.CMS.Client
{
    public partial class UcContent : System.Web.UI.UserControl
    {
        List<DataTable> ds = new List<DataTable>();
        List<DataTable> noduong = new List<DataTable>();
        List<DataTable> noduong_kodiadiem = new List<DataTable>();
        Search s = new Search();
        int tong = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            tong = 0;
             Stopwatch sw = Stopwatch.StartNew();
            String LoaiDV = "";
            String key = "";
            int sort = 0;
            int page =0;
            if (!Page.IsPostBack)
            {
                //thuc hien lay query string 
                if (Request.QueryString["Type"].ToString() != null)
                {
                    LoaiDV = Request.QueryString["Type"].ToString();
                }
                if (Request.QueryString["Key"].ToString() != null)
                {
                    key = Request.QueryString["Key"].ToString();
                }
                if (Request.QueryString["Sort"].ToString() != null)
                {
                    sort = Convert.ToInt32(Request.QueryString["Sort"].ToString());
                }
                if (Request.QueryString["Page"].ToString() != null)
                {
                    page = Convert.ToInt32(Request.QueryString["Page"].ToString());
                }
                s.InPut = key;
                
                //kiem tra dang o seach nao 
                if (LoaiDV != null)
                {
                    try
                    {
                        //truong hop khong chon tinh nang nang cao

                        if (LoaiDV == "19")
                        {
                            DataTable dichvu = new DataTable();
                            dichvu = TUKHOADICHVUBUS.Search2(s);
                            if (dichvu != null)
                            {
                                dichvu.TableName = "DICHVU";
                                ds.Add(dichvu);
                                noduong.Add(dichvu);
                                noduong_kodiadiem.Add(dichvu);
                            }
                        }
                        //KHOA 3-2-2012
                        else
                        {
                            string chuoitatca = "";
                            try
                            {
                                chuoitatca = s.InPut.Substring(0, 3);
                                chuoitatca = chuoitatca.ToLower();
                                if (chuoitatca.Equals("all"))
                                {
                                    sort = 9;
                                }
                            }
                            catch { };
                        }
                        //KHOA tu day len tren
                        //Neu sort = 1 tim theo duong + key + 
                        if (sort == 6)
                        {
                            DataTable dichvu = new DataTable();
                            dichvu = TUKHOADICHVUBUS.Search2(s);
                            if (dichvu != null)
                            {
                                dichvu.TableName = "DICHVU";
                                ds.Clear();
                                ds.Add(dichvu);
                                
                            }
                        }
                        if (sort == 1)
                        {
                            DataTable duongav = new DataTable();
                            duongav = TUKHOADUONGBUS.Search2(s);
                            if (duongav.Rows.Count > 0)
                            {
                                duongav.TableName = "DUONG";
                                noduong.Add(duongav);
                                noduong_kodiadiem.Add(duongav);
                                //KHOA ngay 3-2-2012
                                ds.Add(duongav);
                            }
                        }
                        if (sort != 0 && sort != 4 && sort!=9)
                        {
                            DataTable dd = new DataTable();
                            dd = TUKHOADIADIEMBUS.Search2(s);
                            if (dd != null)
                            {
                                dd.TableName = "TENDIADIEM";
                                ds.Add(dd);
                                noduong.Add(dd);
                            }
                        }
                            //KHOA tu day len tren
                        else if (sort == 2)
                        {
                            DataTable phuongav = new DataTable();
                            phuongav = TUKHOAPHUONGBUS.Search2(s);
                            if (phuongav.Rows.Count > 0)
                            {
                                phuongav.TableName = "PHUONG";
                                noduong.Add(phuongav);
                                noduong_kodiadiem.Add(phuongav);
                                //KHOA ngay 3-2-2012
                                ds.Add(phuongav);
                            }
                        }
                        else if (sort == 3)
                        {
                            DataTable quanhuyenav = new DataTable();
                            quanhuyenav = TUKHOAQUANHUYENBUS.Search2(s);
                            if (quanhuyenav.Rows.Count > 0)
                            {
                                quanhuyenav.TableName = "QUANHUYEN";
                                ds.Add(quanhuyenav);
                                noduong.Add(quanhuyenav);
                                noduong_kodiadiem.Add(quanhuyenav);
                            }
                        }
                        else if (sort == 4)
                        {
                            DataTable ddav = new DataTable();
                            ddav = TUKHOADIADIEMBUS.Search2(s);
                            if (ddav.Rows.Count > 0)
                            {
                                ddav.TableName = "TENDIADIEM";
                                ds.Add(ddav);
                                noduong.Add(ddav);
                                noduong_kodiadiem.Add(ddav);
                            }
                        }
                        else if (sort == 5)
                        {
                            DataTable tinhthanhav = new DataTable();
                            tinhthanhav = TUKHOATINHTHANHBUS.Search2(s);
                            if (tinhthanhav.Rows.Count > 0)
                            {
                                tinhthanhav.TableName = "TINHTHANH";
                                ds.Add(tinhthanhav);
                                noduong.Add(tinhthanhav);
                                noduong_kodiadiem.Add(tinhthanhav);
                            }
                        }
                        //KHOA 3-2-2012
                        else if (sort == 9)
                        {
                            DataColumn[] cl = new DataColumn[] { new DataColumn("MaDichVu") };
                            DataTable dichvu = new DataTable();
                            dichvu.TableName = "DICHVU";
                            dichvu.Columns.AddRange(cl);
                            DataRow row = dichvu.NewRow();
                            row["MaDichVu"] = LoaiDV;
                            dichvu.Rows.Add(row);
                            ds.Add(dichvu);
                            DataTable phuong = new DataTable();
                            phuong = TUKHOAPHUONGBUS.Search2(s);
                            if (phuong != null)
                            {
                                phuong.TableName = "PHUONG";
                                ds.Add(phuong);
                                noduong.Add(phuong);
                                noduong_kodiadiem.Add(phuong);
                            }
                            DataTable quanhuyen = new DataTable();
                            quanhuyen = TUKHOAQUANHUYENBUS.Search2(s);
                            if (quanhuyen != null)
                            {
                                quanhuyen.TableName = "QUANHUYEN";
                                ds.Add(quanhuyen);
                                noduong.Add(quanhuyen);
                                noduong_kodiadiem.Add(quanhuyen);
                            }
                            DataTable duong = new DataTable();
                            duong = TUKHOADUONGBUS.Search3(s);
                            if (duong != null)
                            {
                                duong.TableName = "DUONG";
                                ds.Add(duong);
                            }
                            DataTable tinhthanh = new DataTable();
                            tinhthanh = TUKHOATINHTHANHBUS.Search2(s);
                            if (tinhthanh != null)
                            {
                                tinhthanh.TableName = "TINHTHANH";
                                ds.Add(tinhthanh);
                                noduong.Add(tinhthanh);
                                noduong_kodiadiem.Add(tinhthanh);
                            }
                        }
                        //KHOA tu day len tren
                        else
                        {

                            DataTable phuong = new DataTable();
                            phuong = TUKHOAPHUONGBUS.Search2(s);
                            if (phuong != null)
                            {
                                phuong.TableName = "PHUONG";
                                ds.Add(phuong);
                                noduong.Add(phuong);
                                noduong_kodiadiem.Add(phuong);
                            }
                            DataTable quanhuyen = new DataTable();
                            quanhuyen = TUKHOAQUANHUYENBUS.Search2(s);
                            if (quanhuyen != null)
                            {
                                quanhuyen.TableName = "QUANHUYEN";
                                ds.Add(quanhuyen);
                                noduong.Add(quanhuyen);
                                noduong_kodiadiem.Add(quanhuyen);
                            }
                            DataTable duong = new DataTable();
                            duong = TUKHOADUONGBUS.Search3(s);
                            if (duong != null)
                            {
                                duong.TableName = "DUONG";
                                ds.Add(duong);
                            }
                            DataTable tinhthanh = new DataTable();
                            tinhthanh = TUKHOATINHTHANHBUS.Search2(s);
                            if (tinhthanh != null)
                            {
                                tinhthanh.TableName = "TINHTHANH";
                                ds.Add(tinhthanh);
                                noduong.Add(tinhthanh);
                                noduong_kodiadiem.Add(tinhthanh);
                            }



                            DataTable dd = new DataTable();
                            dd = TUKHOADIADIEMBUS.Search2(s);
                            if (dd != null)
                            {
                                dd.TableName = "TENDIADIEM";
                                ds.Add(dd);
                                noduong.Add(dd);
                            }


                        }
                        
                    }
                    catch { return; }
                      
                   
                    //hien thi phan trang 
                        if (LoaiDV == "19")
                        {
                            Paging(page, LoaiDV, key, true,sort);
                        }
                        else
                        {
                            Paging(page, LoaiDV, key, false,sort);
                        }

                        sw.Stop();
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "setvalue", "setvalue(" + tong + "," + sw.ElapsedMilliseconds.ToString() + ");", true);
                       
                      
                }
            }

        }
        public  void Paging(int page,String type,String key,bool flag,int sort)
        {
            StringBuilder sb = new StringBuilder();
            DataTable dv = new DataTable();
            int sorecordconlai = 5;
            int sosp = 5;
            if (Session["tongso"].ToString() != null)
            {
                if (Convert.ToInt32(Session["tongso"].ToString()) > 0)
                {
                    if (page * sosp > Convert.ToInt32(Session["tongso"].ToString()))
                    {
                        sorecordconlai = (Convert.ToInt32(Session["tongso"].ToString())) - ((page - 1) * sosp);
                    }
                    else
                    {
                        sorecordconlai = sosp;
                    }
                }
            }
           
            sb.Append("<ul>");
            int tongso = 0;
            //kiem tra sort type
            if (sort == 0)
            {
                dv = SearchBUS.SelectALL(ds.ToArray(), type, page, flag, sorecordconlai);
                if (dv == null)
                    dv = SearchBUS.SelectALL(noduong.ToArray(), type, page, flag, sorecordconlai);
                tongso=SearchBUS.sotrang;
                int sotrangtemp = tongso / sosp;

                if (dv == null || ((page * sosp >= tongso) && tongso < 51))
                {
                    DataTable temp = dv;

                    dv = SearchBUS.SelectALL(noduong_kodiadiem.ToArray(), type, page, flag, sorecordconlai);
                    if (page * sosp != tongso)
                    {
                        if (dv == null) dv = temp;
                        else if (temp != null && dv.Rows.Count > temp.Rows.Count)
                        {
                            for (int i = 0; i < temp.Rows.Count; i++)
                            {
                                string madiadiem = temp.Rows[i]["MaTenDiaDiem"].ToString();
                                for (int j = dv.Rows.Count - 1; j >= 0; j--)
                                {
                                    string madiadiemtemp = dv.Rows[j]["MaTenDiaDiem"].ToString();
                                    if (madiadiem.CompareTo(madiadiemtemp) == 0)
                                    {
                                        dv.Rows.RemoveAt(j); break;
                                    }
                                }
                                DataRow r = dv.NewRow();
                                for (int k = 0; k < dv.Columns.Count; k++)
                                {
                                    r[k] = temp.Rows[i][k];
                                }

                                //dv.Rows.Add(r);
                                dv.Rows.InsertAt(r, i);

                            }
                        }
                    }
                }
            }
            else
            {
                //dv = SearchBUS.SelectALL(ds.ToArray(), type, page, flag, sorecordconlai);
                //if (dv == null)
                //    dv = SearchBUS.SelectALL(noduong.ToArray(), type, page, flag, sorecordconlai);
                //if (dv == null)
                //{
                //    dv = SearchBUS.SelectALL(noduong_kodiadiem.ToArray(), type, page, flag, sorecordconlai);
                //}
                //KHOA ngay 3-2-2012
                dv = SearchBUS.SelectALL(ds.ToArray(), type, page, flag, sorecordconlai);
                if (dv == null)
                    dv = SearchBUS.SelectALL(noduong.ToArray(), type, page, flag, sorecordconlai);
                tongso = SearchBUS.sotrang;
                int sotrangtemp = tongso / sosp;

                if (dv == null || ((page * sosp > tongso) && tongso < 51))
                {
                    DataTable temp = dv;

                    dv = SearchBUS.SelectALL(noduong_kodiadiem.ToArray(), type, page, flag, sorecordconlai);
                    if (page * sosp != tongso)
                    {
                        if (dv == null) dv = temp;
                        else if (temp != null && dv.Rows.Count > temp.Rows.Count)
                        {
                            for (int i = 0; i < temp.Rows.Count; i++)
                            {
                                string madiadiem = temp.Rows[i]["MaTenDiaDiem"].ToString();
                                for (int j = dv.Rows.Count-1; j >=0 ; j--)
                                {
                                    string madiadiemtemp = dv.Rows[j]["MaTenDiaDiem"].ToString();
                                    if (madiadiem.CompareTo(madiadiemtemp) == 0)
                                    {
                                        dv.Rows.RemoveAt(j); break;
                                    }
                                }
                                DataRow r = dv.NewRow();
                                for (int k = 0; k < dv.Columns.Count; k++)
                                {
                                    r[k] = temp.Rows[i][k];
                                }

                                //dv.Rows.Add(r);
                                dv.Rows.InsertAt(r, i);

                            }
                        }
                    }
                }
            }
            try { tongso = SearchBUS.sotrang; }
            catch { };
            rpthienthi.DataSource = dv;
            rpthienthi.DataBind();

            Session["tongso"] = tongso;
            tong = tongso;
            int sotrang = tongso / sosp;
            //if (sotrang == 0) sotrang = 1;
            if (tongso == 1)
                sotrang = 0;
            else
            {
                if (sotrang * sosp < tongso)
                    sotrang++;
            }
            int prev = 0;
            int next = 0;
            // su ly luu trang hien tai 
            //kiem tra số trang hiện tại nếu trang hiện tại là 10 thì next
            //luu last trang 
            int sotranghienthichophep = 10;
            if (sotrang < sotranghienthichophep)
                sotranghienthichophep = sotrang;
            if (page == 1)
            {
                if (sotrang != 0)
                {

                    if (page >= 2)
                    {
                        prev = page - 1;
                        sb.Append(" <li><a href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort="+sort+"&Page=" + prev.ToString() + "' >Prev</a></li> ");
                    }
                    for (int i = 1; i <= sotranghienthichophep; i++)
                    {

                        if (i == page)
                        {
                            sb.Append(" <li><a class='curentpage' href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort="+sort+"&Page=" + page.ToString() + "' >" + i.ToString() + "</a></li> ");
                        }
                        else
                        {
                            sb.Append(" <li><a href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort="+sort+"&Page=" + i.ToString() + "' >" + i.ToString() + "</a></li> ");
                        }
                    }
                    if (page < sotrang)
                    {
                        next = page + 1;
                        sb.Append(" <li><a href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort=" + sort + "&Page=" + next.ToString() + "' >Next</a></li> ");
                    }
                }
            }
            if (page <= sotranghienthichophep && page != 1)
            {
                if (sotrang > 0)
                {
                    if (page <= sotrang)
                    {
                        if (page >= 2)
                        {
                            prev = page - 1;
                            sb.Append(" <li><a href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort=" + sort + "&Page=" + prev.ToString() + "' >Prev</a></li> ");
                        }
                        for (int i = 1; i <= sotranghienthichophep; i++)
                        {

                            if (i == page)
                            {
                                sb.Append(" <li><a class='curentpage' href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort=" + sort + "&Page=" + page.ToString() + "' >" + i.ToString() + "</a></li> ");
                            }
                            else
                            {
                                sb.Append(" <li><a href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort=" + sort + "&Page=" + i.ToString() + "' >" + i.ToString() + "</a></li> ");
                            }
                        }
                        if (page < sotrang)
                        {
                            next = page + 1;
                            sb.Append(" <li><a href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort=" + sort + "&Page=" + next.ToString() + "' >Next</a></li> ");
                        }
                    }
                }
            }
            if (page > sotranghienthichophep && page != 1)
            {
                if (sotrang > 0)
                {
                    int n = 0;
                    if (sotrang % sotranghienthichophep != 0)
                    {
                        n = (sotrang / sotranghienthichophep) + 1;
                    }
                    else
                    {
                        n = sotrang / sotranghienthichophep;
                    }

                    int tem = page / sotranghienthichophep;
                    int vitritiep = sotranghienthichophep * tem;
                    if (page == (sotranghienthichophep * tem))
                    {
                        vitritiep = sotranghienthichophep * (tem - 1);
                        sotranghienthichophep = (sotranghienthichophep * (tem));
                    }
                    else
                    {
                        if ((sotranghienthichophep * tem) + 10 <= sotrang)
                        {
                            sotranghienthichophep = (sotranghienthichophep * tem) + 10;
                        }
                        else
                        {
                            int soconlai = sotrang - (sotranghienthichophep * tem);
                            sotranghienthichophep = (sotranghienthichophep * tem) + soconlai;
                        }
                    }
                    if (page <= sotrang)
                    {
                        if (page >= 2)
                        {
                            prev = page - 1;
                            sb.Append(" <li><a href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort=" + sort + "&Page=" + prev.ToString() + "' >Prev</a></li> ");
                        }
                        for (int i = vitritiep + 1; i <= sotranghienthichophep; i++)
                        {

                            if (i == page)
                            {
                                sb.Append(" <li><a class='curentpage' href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort=" + sort + "&Page=" + page.ToString() + "' >" + i.ToString() + "</a></li> ");
                            }
                            else
                            {
                                sb.Append(" <li><a href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort=" + sort + "&Page=" + i.ToString() + "' >" + i.ToString() + "</a></li> ");
                            }
                        }
                        if (page < sotrang)
                        {
                            next = page + 1;
                            sb.Append(" <li><a href='DetailSearch.aspx?Type=" + type + "&Key=" + key + "&Sort=" + sort + "&Page=" + next.ToString() + "' >Next</a></li> ");
                        }

                    }
                }
            }
            sb.Append("</ul>");
            lbphantrang.Text = sb.ToString();
           
        }
        public  void TUKHOADIADIEM()
        {
            DataTable news = new DataTable();
            news = TUKHOADIADIEMBUS.Search(s);
            if (news!=null)
            {
                ds.Add(news);
            }
        }
        public void TUKHOAPHUONG()
        { 
            ds.Add(TUKHOAPHUONGBUS.Search(s));
        }
        public void TUKHOAQUANHUYEN()
        { 
            ds.Add(TUKHOAQUANHUYENBUS.Search(s));
        }
        public void TUKHOADUONG()
        {
            ds.Add(TUKHOADUONGBUS.Search(s));
        }
        public void TUKHOATINHTHANH()
        { 
            ds.Add(TUKHOATINHTHANHBUS.Search(s));
        }
        public void TUKHOADICHVU()
        {
            ds.Add(TUKHOADICHVUBUS.Search(s));
        }
    }
}